package com.example.news_reader;

import java.io.InputStream;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class DataList {

	private String name;
	private String link;
	private String img;
	private String sub;
	private String date;

	DataList(String name, String link, String sub, String date){
		this.name = name;
		this.link = link;
		this.sub = sub;
		this.date = date;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getImg(int num) {
		return img;
	}

	public Bitmap getImg() {
		Bitmap b=null;
		
		if((sub.indexOf("src=\"")==-1)&&(sub.indexOf("src=\"")==-1)){
			img=null;
		}else if(!(sub.substring(0,1).equals("<"))){
			img=null;
		}else if(sub.indexOf("src=\'")!=-1){
			img = sub.substring(sub.indexOf("src=\'")+5);
			img = img.substring(0,img.indexOf("\'"));
			if(!(img.substring(0,4).equals("http"))){
				img = "http:"+img;
			}
		}else{
			img = sub.substring(sub.indexOf("src=\"")+5);
			img = img.substring(0,img.indexOf("\""));
			if(!(img.substring(0,4).equals("http"))){
				img = "http:"+img;
			}
		}

		try{
			if(img!=null){
				InputStream is=new URL(img).openStream();
				Bitmap bit = BitmapFactory.decodeStream(is);
				b = Bitmap.createScaledBitmap(bit, 75, 75, false);
				is.close();
			}else{
			}
		}catch (Exception e){
			Log.e("dd2","",e);
		}

		return b;
	}

	public String getSub() {
		String d;
		sub = "<html><head>"+
				"<meta http-equiv=\"Content-Type\" "+
				"content=\"text/html; charset=UTF-8\">"+
				"</head>"+
				sub+"</body></html>";

		if(sub.substring(sub.indexOf("src=\"")+5,sub.indexOf("src=\"")+9).equals("http")){
			d=sub;
		}else{
			d = sub.substring(0,sub.indexOf("src=\"")+5)+
					"http:"+
					sub.substring(sub.indexOf("src=\"")+5);
		}

		return d;
		//		return sub;
	}

	public void setSub(String sub) {
		this.sub = sub;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getLink() {
		if(link!=null){
			if(link.length()>3){
				if(link.subSequence(0, 3).equals("url"))
					link=link.substring(4);
			}
		}
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}

}
